package tt.dz.util.Nosql.Redis.Template;

import java.io.IOException;

import org.msgpack.MessageTypeException;
import org.msgpack.packer.Packer;
import org.msgpack.template.AbstractTemplate;
import org.msgpack.unpacker.Unpacker;

/**
 * 模版
 * @author peizhide
 *
 */
@SuppressWarnings("rawtypes")
public class RedisTemplate extends AbstractTemplate{
	
	private Class cls;
	
	public RedisTemplate(){
	}
	
	public RedisTemplate(Class cls){
		this.cls=cls;
	}
	
	public void write(Packer pk, Object v, boolean required) throws IOException {
		if (cls == null) {
            if (required) {
                throw new MessageTypeException("Attempted to write null!");
            }
            pk.writeNil();
            return;
        }
        pk.write(cls);
	}
	
	@SuppressWarnings("unchecked")
	public Object read(Unpacker u, Object to, boolean required) throws IOException {
		 if (!required && u.trySkipNil()) {
	            return null;
	        }
			return u.read(cls);
	}
}
